package org.abyss.upload;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.abyss.upload.resolver.DefaultUploadResolverFactory;
import org.abyss.upload.resolver.UploadResolver;
import org.abyss.upload.resolver.UploadResolverFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.multipart.MultipartFile;

import edu.tgc.entity.file.FilePath;
import edu.tgc.entity.file.FileResource;

public class AjaxUploadService implements InitializingBean {

    protected JpaRepository<FileResource, UUID> baseRespository;
    protected Map<String, UploadResolver> resolvers = new HashMap<>();
    protected UploadResolverFactory uploadResolverFactory;

    @Override
    public void afterPropertiesSet() throws Exception {
        if (this.uploadResolverFactory == null) {
            this.uploadResolverFactory = new DefaultUploadResolverFactory();
        }
    }

    public FileMetaData download(String id) {
        FileResource resource = this.baseRespository.findOne(UUID.fromString(id));
        if (resource == null) {

        } else {
            FilePath path = new FilePath();
            BeanUtils.copyProperties(resource, path);
            try {
                this.uploadResolverFactory.build(path).read(id, path);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public JpaRepository<FileResource, UUID> getBaseRespository() {
        return this.baseRespository;
    }

    public UploadResolverFactory getUploadResolverFactory() {
        return this.uploadResolverFactory;
    }

    @Autowired
    public void setBaseRespository(JpaRepository<FileResource, UUID> baseRespository) {
        this.baseRespository = baseRespository;
    }

    public void setUploadResolverFactory(UploadResolverFactory uploadResolverFactory) {
        this.uploadResolverFactory = uploadResolverFactory;
    }

    public FileMetaData upload(MultipartFile source, FilePath path) {
        try {
            this.uploadResolverFactory.build(path).write(source, null, path);
            FileResource resource = new FileResource();
            String originalFilename = source.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            if (lastIndexOf != -1) {
                resource.setName(originalFilename.substring(0, lastIndexOf));
                resource.setExt(originalFilename.substring(lastIndexOf + 1));
            } else {
                resource.setName(originalFilename);
            }
            resource.setSize(source.getSize());
            // resource.setPath(path);

            this.baseRespository.save(resource);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
